package com.qf.sale.controller;

import com.qf.lw.code.ResponseShopCar;
import com.qf.lw.pojo.GoodsPojo;
import com.qf.sale.service.IGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
@RestController
public class ShopController {
    @Autowired
    IGoodsService goodsService;
    //添加商品到购物车
    @RequestMapping("addShopCar")
    public ResponseShopCar addShopCar(HttpSession session, String gid){
        //从session中取shopCar
        Map<String,GoodsPojo> shopCar= (Map<String, GoodsPojo>) session.getAttribute("shopCar");
        //如果购物车为空则新建购物车
        if(null==shopCar){
            shopCar=new HashMap<>();
        }
        //判断购物车是否有当前添加的商品
        //queryGoodsDetail（）此方法就是通过id去查询上屏的信息
        GoodsPojo goodsPojo=shopCar.get(gid);
        if(null==goodsPojo){
           goodsPojo= goodsService.queryGoodsDetail(gid);
           shopCar.put(gid,goodsPojo);
        }else{
            goodsPojo.setNumber(goodsPojo.getNumber()+1);
        }
        //添加成功后，我们要将这个shopcar 存到session中
        session.setAttribute("shopCar",shopCar);
        //构建返回参数
        ResponseShopCar rsc=new ResponseShopCar();
        //计算当前的数量和价格
        rsc.setCurrentNumber(goodsPojo.getNumber());
        rsc.setCurrentPrice(goodsPojo.getNumber()*goodsPojo.getGprice());
        //计算当前商品的总数量和总价格
        putTotalAndPrice(shopCar,rsc);
        return  rsc;
    }
    //购物车减
    @RequestMapping("subShopCar")
    public ResponseShopCar subShopCar(HttpSession session,String gid){
        //构建返回参数
        ResponseShopCar rsc=new ResponseShopCar();
        //获取购物车
      Map<String,GoodsPojo>  map= (Map<String, GoodsPojo>) session.getAttribute("shopCar");
        //如果购物车不存在，怎无法减
        if(null==map){
            rsc.setResultCode("00001");
            rsc.setResultMsg("系统出错，请稍后再试");
        }
        GoodsPojo goodsPojo=map.get(gid);
        if(null==goodsPojo){
            rsc.setResultCode("00002");
            rsc.setResultMsg("该商品不存在");
        }
        //商品数量大于1的情况可以直接减
        if(goodsPojo.getNumber()>1){
            goodsPojo.setNumber(goodsPojo.getNumber()-1);
            //当前数据行的数量和总价格
            rsc.setCurrentNumber(goodsPojo.getNumber());
            rsc.setCurrentPrice(goodsPojo.getNumber()*goodsPojo.getGprice());
        }else{
            rsc.setResultCode("00009");
            map.remove(gid);//一处这个商品
        }

        //计算当前购物车的总数量和总价格
        putTotalAndPrice(map,rsc);


      return rsc;
    }
    private void putTotalAndPrice(Map<String,GoodsPojo> shopCar, ResponseShopCar rsc) {
        Set<String> keys =shopCar.keySet();
        int total=0;
        float price=0.0f;
        //计算总数和总价
        for(String key:keys){
                total+=shopCar.get(key).getNumber();
                price+=shopCar.get(key).getNumber()*shopCar.get(key).getGprice();
        }
        rsc.setTotal(total);
        rsc.setTotalPrice(price);
    }
    //清空购物车
    @RequestMapping("cleanShopCar")
    public ModelAndView clearShopCar(HttpSession session){
        ModelAndView mv=new ModelAndView();
        session.removeAttribute("shopCar");
        mv.setViewName("shopcart");
        return mv;
    }

}
